home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / newmarch.zip / HELLO.C < prev    next >
C/C++ Source or Header  |  1992-09-08  |  4KB  |  164 lines

  1. /* Author:   $Author: jan $
  2.  * File:     $Source: /usr/usrs/jan/desktop/X_Book.boo/programs/RCS/hello.c,v $
  3.  * Date:     $Date: 1992/09/09 00:10:01 $
  4.  * Revision: $Revision: 1.1 $
  5.  */
  6.  
  7. #include "copyright.h"
  8.  
  9. #include <stdio.h>    
  10. #include <X11/Xlib.h>    
  11. #include <X11/Xutil.h>    
  12.     
  13. /*    
  14. ** Constants    
  15. */    
  16. char WINDOW_NAME[] = "Window";    
  17. char ICON_NAME[] = "Icon";    
  18.     
  19. /*    
  20. ** Globals    
  21. */    
  22. Display *display;  /* the display device */    
  23. int     screen;  /* the screen on the display */    
  24. Window  main_window;    
  25. GC      gc;    
  26. unsigned long  foreground, background;
  27.  
  28. void
  29. initX ()    
  30. {    
  31.     /* set the display name from the environment
  32.        vbl DISPLAY */    
  33.     display = XOpenDisplay (NULL);    
  34.     if (display == NULL)    
  35.      {    fprintf (stderr, "Unable to open display %s\n",
  36.                 XDisplayName (NULL));    
  37.         exit (1);    
  38.     }    
  39.     screen = DefaultScreen (display);    
  40.         
  41.     /* use the default foreground 
  42.        and background colors */
  43.     foreground = BlackPixel (display, screen);    
  44.     background = WhitePixel (display, screen);    
  45. }
  46.  
  47. /*    
  48. ** Opens a window on the display device, and returns    
  49. ** the window ID.    
  50. **    
  51. ** It takes (x,y) coords, the width and height of the
  52. ** window, and the width of the border    
  53. */    
  54. Window    
  55. openWindow (x, y, width, height,
  56.             border_width, argc, argv)    
  57.     int x, y;            /* coords of the upper left
  58.                            corner in pixels */    
  59.     int width, height;    /* size of the window 
  60.                            in pixels */
  61.     int border_width;    /* the border width is not
  62.                            included in the    
  63.                            other dimensions */    
  64.     int argc;    
  65.     char **argv;    
  66. {    
  67.     Window  new_window;    
  68.     XSizeHints  size_hints;    
  69.         
  70.     /* now create the window */    
  71.     new_window = XCreateSimpleWindow (display,     
  72.                        DefaultRootWindow (display),    
  73.                        x, y, width, height,    
  74.                        border_width,    
  75.                        foreground, background);    
  76.             
  77.     /* set up the size hints for the window manager */
  78.     size_hints.x = x;    
  79.     size_hints.y = y;    
  80.     size_hints.width = width;    
  81.     size_hints.height = height;    
  82.     /* and state what hints are included */    
  83.     size_hints.flags = PPosition | PSize;    
  84.     
  85.     /* let the window manager know about the window */
  86.     XSetStandardProperties (display, new_window,    
  87.                             WINDOW_NAME, ICON_NAME,    
  88.                             None,      /* no icon map */
  89.                             argv, argc, &size_hints);
  90.             
  91.     /* Return the window ID */    
  92.     return (new_window);    
  93. }    
  94.     
  95. /*    
  96. ** Create a graphics context using default values, and
  97. ** return it in the pointer gc
  98. */    
  99. GC
  100. getGC ()    
  101. {   GC gc;    
  102.     XGCValues gcValues;    
  103.     
  104.     gc = XCreateGC (display, main_window,     
  105.                 (unsigned long) 0, &gcValues);    
  106.     
  107.     XSetBackground (display, gc, background);    
  108.     XSetForeground (display, gc, foreground);    
  109.     return (gc);
  110. }    
  111.     
  112. /*    
  113. ** Terminate the program gracefully    
  114. */    
  115. quitX ()    
  116. {    
  117.     XCloseDisplay (display);    
  118.     exit (0);    
  119. }    
  120.        
  121. /*    
  122. ** Write a string    
  123. ** and draw a circle   
  124. */    
  125. display_something ()    
  126. {   /* Ignore these three lines for now */
  127.     XEvent event;
  128.     XSelectInput (display, main_window, ExposureMask);
  129.     XNextEvent (display, &event);
  130.     /* end of ignore lines */
  131.  
  132.     /* the proverbial string */    
  133.     XDrawImageString (display, main_window, gc,    
  134.                       10, 10, "Hello world",    
  135.                       strlen ("Hello world"));    
  136.     
  137.     /* and a world (circle) to go with it */    
  138.      XDrawArc (display, main_window, gc,    
  139.                30, 30,
  140.                100, 100, 
  141.                0, 360*64);
  142.     XFlush (display);
  143. }    
  144.     
  145. main (argc, argv)    
  146.     int argc;    
  147.     char **argv;    
  148. {
  149.     initX ();    
  150.     
  151.     main_window = openWindow (10, 20, 500, 400,
  152.                                5, argc, argv);    
  153.     
  154.     gc = getGC ();    
  155.     
  156.     /* Display the window on the screen */    
  157.     XMapWindow (display, main_window);    
  158.     
  159.     display_something ();    
  160.     sleep (30);    
  161.     
  162.     quitX ();    
  163. }     
  164.